package main

import (
	"os"
	"strings"
	"net/http"
	"path/filepath"
	"github.com/gin-gonic/gin"
	"github.com/unrolled/secure"
)

func TlsHandler() gin.HandlerFunc {
	return func(context *gin.Context) {
		secureMiddleware := secure.New(secure.Options{
			SSLRedirect: true,
			SSLHost: "codebai.cn:443",
		})
		err := secureMiddleware.Process(context.Writer, context.Request)

		// If there was an error, do not continue.
		if err != nil {
			return
		}
		context.Next()
	}
}
func main() {
	router := gin.Default()
	//使用https
	//router.Use(TlsHandler())
	//加载静态文件
	router.Static("/img","./img")
	router.Static("/css","./css")
	//载入所有模板 对于多级目录不能使用
	//router.LoadHTMLGlob("daka/*")
	//router.LoadHTMLGlob("blog/*")
	router.LoadHTMLFiles("index.html")

	var files []string
	filepath.Walk("blog", func(path string, info os.FileInfo, err error) error {
		if strings.HasSuffix(path, ".html") {
			files = append(files, path)
		}
		return nil
	})

	//router.LoadHTMLFiles(files...)

	//设置根路由
	router.GET("/",func(context *gin.Context) {
		context.HTML(http.StatusOK, "index.html", gin.H{})
	})
	router.NoRoute(func(context *gin.Context) {
		context.Header("Content-Type", "text/html; charset=utf-8")
		context.String(http.StatusOK, "<h3>访问的页面不存在</h3>")
	})
	router.Run(":80")
	//router.RunTLS(":443", "codebai.cn_bundle.pem", "codebai.cn.key")
}

